package com.ns.sbs.nth;

import com.ns.basic.log.utils.ApiLogUtil;
import com.ns.core.utils.sg.ResultOk;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 404相关操作
 *
 * @author lw
 */
@RestController
@CrossOrigin
public class NotFoundController implements ErrorController {

    /**
     * 除404以外的异常都会被全局异常处理掉，走到这里的请求都是404
     *
     * @param request  req
     * @param response res
     */
    @GetMapping("/error")
    public Object error(HttpServletRequest request, HttpServletResponse response) {
        // 记录API访问日志
        ResultOk aj = ResultOk.get(404, "not found");
        ApiLogUtil.endRequest(aj);
        // 返回json消息
        response.setStatus(200);
        return aj;
    }

}